<template>
  <a-modal
    title="操作"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
  >


    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <!--companyId-->

        <a-form-item :label-col="labelCol" :wrapper-col="wrapperCol" label="所属公司">
          <a-select :disabled="isDisabled"
            v-decorator="[ 'companyId', {rules: [{ required: true, message: '请输入商户名称' }],initialValue: defaultCompany}]"
            placeholder="请选择所属公司">
            <a-select-option :value="company.id" v-for="company in companyList" :key="company.id">
              {{company.name}}
            </a-select-option>
          </a-select>
        </a-form-item>


        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="类别名称">
          <a-input placeholder="类别名称" v-decorator="[ 'name', {rules: [{ required: true, message: '请输入类别名称' }]}]"
          ></a-input>
        </a-form-item>



      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { save } from '../../../api/business/category'
  import { selectAllCompany } from '../../../api/system/company'
  import pick from 'lodash.pick'

  export default {
    //--############  data() ###############-->
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        visible: false,
        confirmLoading: false,
        mdl: {},
        form: this.$form.createForm(this),

        isDisabled: false,

        companyList: [],
        defaultCompany:''
      }
    },
    //--############  data() ###############-->

    created() {
      this.getAllCompany()
    },

    //--############  methods() ###############-->
    methods: {
      getAllCompany: function() {
        selectAllCompany().then(res => {
          if (res.code === 200) {
            this.companyList = res.result;
            this.defaultCompany = this.companyList[0].id
          } else {
            this.$notification.error({
              message: '错误',
              description: res.message,
              duration: 4
            })
          }
        })
      },
      add() {
        this.isDisabled = false
        this.visible = true
      },
      edit(record) {
        this.isDisabled = true
        this.mdl = Object.assign({}, record)
        this.visible = true
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.mdl, 'companyId', 'name', 'address', 'contact', 'mobile', 'telephone'))
        })
      },
      close() {
        this.$emit('close')
        this.visible = false
        this.form.resetFields()
        this.mdl = {}
      },
      handleOk() {
        const _this = this
        // 触发表单验证
        this.form.validateFields((err, values) => {
          // 验证表单没错误
          if (!err) {
            values.id = this.mdl.id
            _this.confirmLoading = true
            // 模拟后端请求 2000 毫秒延迟
            save(values).then((res) => {
              if (res.code === 200) {
                _this.$message.success('保存成功')
                _this.$emit('ok')
                _this.close()
              } else {
                this.$notification.error({
                  message: '错误',
                  description: res.message,
                  duration: 4
                })
              }
            }).catch(() => {
              // Do something
            }).finally(() => {
              _this.confirmLoading = false
            })
          }
        })
      },
      handleCancel() {
        this.close()
      }
    }
    //--############  methods() ###############-->
  }
</script>

<style scoped>

</style>
